<!DOCTYPE html>
<html>

{include file='public/header'}

<body>
    <form class="layui-form" style="margin:20px 0;" lay-filter="formAjax">
        <div class="layui-form-item">
            <label class="layui-form-label">提交类型：</label>
            <div class="layui-input-block">
                <input type="radio" name="submit_type" lay-filter="submit_type" value="one" title="单个" checked>
                <input type="radio" name="submit_type" lay-filter="submit_type" value="batch" title="批量">
            </div>
        </div>
        <div class="layui-form-item" style='display: flex;'>
            <label class="layui-form-label">
                <span class="x-red">*</span>选择小程序
            </label>
            <div id="GetAuthorizers" style="width: 100%;flex:1;"></div>
        </div>
    </form>
    <table id="tabled" lay-filter="tabled"></tbody>
    </table>
    <script type="text/html" id="tbBar">
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">添加</a>
    </script>
    <script>
        var applets = []
        layui.use(["form",'table'], function () {
            var table = layui.table,
                form = layui.form;
            table.render({
                elem: '#tabled' //指定原始表格元素选择器（推荐id选择器）
                ,id:'tabled'
                ,cols: [[
                    { field: 'name', title: '模板名称'},
                    { field: 'tid', title: '模板ID'},
                    { field: 'kidList', title: '关键词'},
                    { field: 'sceneDesc', title: '描述'},
                    { toolbar: '#tbBar', title: '操作'}
                ]] //设置表头
                ,data:[
                    {name:'剧集更新提醒',tid:'44911',kidList:[1,2,3,4],sceneDesc:'剧集更新提醒'},
                    {name:'新剧上线提醒',tid:'45057',kidList:[1,3,4],sceneDesc:'新剧上线提醒'},
                    {name:'签到提醒',tid:'45056',kidList:[4,5,6],sceneDesc:'签到提醒'},
                ]
            });
            table.on('tool(tabled)', function (obj) {
                var event = obj.event;
                if (event === 'add') {
                    if(applets.length === 0){
                        layer.msg('请选择小程序后再操作',{icon:5,time:1000});
                    }else{
                        if(form.val("formAjax").submit_type == 'one'){
                            AnyAjax('确定要提交该模板吗？','/admin/OpenWeixin/postMessageTemplate',{...obj.data,app_id:applets.toString()},'tabled')
                        }else{
                            batchTask('确定要提交该模板吗？',{
                                app_ids:form.val("formAjax").app_id.split(','),
                                task_method:'postMessageTemplate',
                                task_data:obj.data
                            })
                        }
                    }
                }
            });
            form.on('radio(submit_type)', function(data){
                let app_id = form.val("formAjax").app_id.split(',');
                if(data.value == 'one'){
                    xmSelectGetAuthorizersOption.initValue = app_id.length == 0 ? '' : [app_id[0]]
                    xmSelectGetAuthorizersOption.radio = true;
                    xmSelectGetAuthorizersOption.clickClose = true;
                    xmSelectGetAuthorizersOption.label = {
                        type:'text'
                    };
                }else{
                    xmSelectGetAuthorizersOption.initValue = app_id ?? ''
                    xmSelectGetAuthorizersOption.radio = false;
                    xmSelectGetAuthorizersOption.clickClose = false;
                    xmSelectGetAuthorizersOption.label = {
                        type:'block'
                    };
                }
                xmSelectA(xmSelectGetAuthorizersOption)
            });
        });
        let xmSelectGetAuthorizersOption = {
            initValue:'',//初始值
            el:'#GetAuthorizers',
            url:'/admin/OpenWeixin/getAuthorizersSelect',
            direction:'down',//下拉方向
            isData:true,
            prop:{
                name:'app_name',
                value:"app_id"
            },
            name:'app_id',
            radio:true,
            clickClose:true,
            label: {
                type: 'text'
            },
            pageSize:5,
            onFn:function(data){
                applets = [];
                data.arr.forEach(item=>{
                    applets.push(item.app_id)
                })
            }
        }
        xmSelectA(xmSelectGetAuthorizersOption)
    </script>
</body>

</html>